package com.tengju.data.domain.model.template;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by develop at 2020/9/9.
 * @author develop
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SubsidyTemplate {

    /**
     * 补贴模版Id
     */
    private Long subsidyTemplateId;

    /**
     * 模版名称
     */
    private String subsidyTemplateName;

    /**
     * 模版别名（给商城看的）
     */
    private String subsidyTemplateAlias;

    /**
     * 模板系数明细
     */
    @Builder.Default
    private List<SubsidyTemplateRatio> subsidyTemplateRatios = new ArrayList<>(10);

    /**
     * 模版创建时间
     */
    private LocalDateTime createTime;

    /**
     * 根据业绩获取补贴系数
     * @param achievement
     * @return
     */
    public Double getDebate(BigDecimal achievement) {
        if (CollectionUtils.isEmpty(subsidyTemplateRatios)) {
            return 0D;
        }
        List<SubsidyTemplateRatio> orderList = subsidyTemplateRatios.stream()
                .sorted(Comparator.comparingDouble(v -> v.getMinPrice() + v.getRebate() * 100))
                .collect(Collectors.toList());
        for (SubsidyTemplateRatio temp : orderList) {
            if (temp.isMeet(achievement)) {
                return temp.getRebate();
            }
        }
        return 0D;
    }
}
